<?php
class ProductsController extends AppController{
    public $parent_code = 'danh-muc-san-pham';
    function index($child_code = null)
    {
        $lang_code = $this->Session->read('lang_code');
        $this->loadModel('Category'); $this->Category->lang_code = $lang_code;
        $this->loadModel('Product'); $this->Product->lang_code = $lang_code;
        
        $parent_code = $this->parent_code;
        
        $parent_id = $this->Category->get_category_id_by_code($parent_code);
        $parent_info = $this->Category->get_info_category_by_id($parent_id);
        $meta_title = $parent_info['category_desc']['meta_title'];
        $meta_keywords = $parent_info['category_desc']['meta_keywords'];
        $meta_description = $parent_info['category_desc']['meta_description'];
        $this->set('meta_title', $meta_title); $this->set('meta_keywords', $meta_keywords); $this->set('meta_description', $meta_description);

        $has_html = '';
        $this->set('has_html', $has_html);
        $header_menu_sidebar['title'] = $parent_info['category_desc']['title'];
        $header_menu_sidebar['link'] = $parent_info['link']['link'];
        $this->set('header_menu_sidebar', $header_menu_sidebar);
        $menu_sidebar = $this->Category->get_category_by_code($parent_code);
        $this->set('menu_sidebar', $menu_sidebar);

        $this->set('parent_code', $parent_code);
        
        
        if($child_code == null)
        {
            $number_page = $this->Product->get_number_page('cutkit');
            $page_current = 1; $start = 0; $limit = Configure::read('product_per_page');
            if(isset($_GET['page']))
            {
                $page_current = $_GET['page'];
                $page_current = max(1, round($page_current));
                $page_current = min($number_page, $page_current);
            }
    
            $this->set('number_page', $number_page);
            $this->set('page_current', $page_current);
            
            if($page_current != '')
            {
                $start = ($page_current - 1) * $limit;
            }
            
            $products = $this->Product->get_product_by_type('cutkit', array(), $start, $limit);
            $this->set('products', $products);
            
            $breadcrumbs[] = array('title' => $parent_info['category_desc']['title'], 'link' => $parent_info['link']['link']);
        }
        else
        {
            $category_id = $this->Category->get_category_id_by_code($child_code);
            $this->set('category_id', $category_id);
            $category_info = $this->Category->get_info_category_by_id($category_id);
            if(empty($category_info))
                $this->redirect(array('controller' => 'products', 'action' => 'index'));
            $this->set('category_info', $category_info);
            
            $list_category = $this->Category->get_all_id_category();
            $filter_category = $this->Category->filter_category($list_category);
            $in_array_cate = $this->Category->filter_category_by_id($filter_category, $category_id);
            
            $number_page = $this->Product->get_number_page('cutkit', $in_array_cate);
            $page_current = 1; $start = 0; $limit = Configure::read('product_per_page');
            if(isset($_GET['page']))
            {
                $page_current = $_GET['page'];
                $page_current = max(1, round($page_current));
                $page_current = min($number_page, $page_current);
            }
    
            $this->set('number_page', $number_page);
            $this->set('page_current', $page_current);
            
            if($page_current != '')
            {
                $start = ($page_current - 1) * $limit;
            }
                        
            $products = $this->Product->get_product_by_type('cutkit', $in_array_cate, $start, $limit);
            $this->set('products', $products);
            
            
            $breadcrumbs[] = array('title' => $parent_info['category_desc']['title'], 'link' => $parent_info['link']['link']);
            $breadcrumbs[] = array('title' => $category_info['category_desc']['title'], 'link' => $parent_info['link']['link'] .'/'. $category_info['link']['link']);
        
            
            $this->set('cate_code_active', $category_info['category']['category_code']);
        }
        $this->set('breadcrumbs', $breadcrumbs);
        $products_favorite = $this->Product->get_product_favorite('cutkit');
        $this->set('products_favorite', $products_favorite);
    }
    
    function detail($product_id = null)
    {
        $parent_code = $this->parent_code;
        if($product_id == null)
            $this->redirect(array('controller' => 'products', 'action' => 'index'));
        
        $lang_code = $this->Session->read('lang_code');
        $this->loadModel('Category'); $this->Category->lang_code = $lang_code;
        $this->loadModel('Product'); $this->Product->lang_code = $lang_code;
        $this->loadModel('Image'); $this->Image->lang_code = $lang_code;
        $this->loadModel('Menu'); $this->Menu->lang_code = $lang_code;
        
        $product_info = $this->Product->get_info_product('cutkit', $product_id);
        if(empty($product_info))
            $this->redirect(array('controller' => 'products', 'action' => 'index'));            
            
        $images = $this->Image->get_image('product', $product_id);
        
        $product_compo = $this->Product->get_compo_by_product_id($product_id);
        $this->set('product_compo', $product_compo);
        
        $this->set('product_info', $product_info[0]);
        $this->set('images', $images);
        
        $cate_id_cutkit_camket = $this->Category->get_category_id_by_code('cut_kit_cam_ket');
        $cate_info_cutkit_camket = $this->Category->get_info_category_by_id($cate_id_cutkit_camket);
        $this->set('cate_info_cutkit_camket', $cate_info_cutkit_camket);
        
        $products_favorite = $this->Product->get_product_favorite('cutkit');
        $this->set('products_favorite', $products_favorite);
        
        $favorite = array();
        if($this->Cookie->read('favorite'))
           $favorite = $this->Cookie->read('favorite');
        $this->set('favorite', $favorite);
        
  
        $parent_id = $this->Category->get_category_id_by_code($parent_code);
        $parent_info = $this->Category->get_info_category_by_id($parent_id);
        $breadcrumbs[] = array('title' => $parent_info['category_desc']['title'], 'link' => $parent_info['link']['link']);
        $link = $parent_code . '/'. $product_info[0]['custom']['link'];
        $breadcrumbs[] = array('title' => $product_info[0]['custom']['title'], 'link' => $link);
        $this->set('breadcrumbs', $breadcrumbs);
        
        $this->set('cate_code_active', $product_info[0]['custom']['link']);
        
        $meta_title = $product_info[0]['product_desc']['meta_title'];
        $meta_keywords = $product_info[0]['product_desc']['meta_keywords'];
        $meta_description = $product_info[0]['product_desc']['meta_description'];
        $this->set('meta_title', $meta_title);
        $this->set('meta_keywords', $meta_keywords);
        $this->set('meta_description', $meta_description);

        $menu_ct_san_pham = $this->Menu->get_menu_by_code('menu-chi-tiet-san-pham');
        $this->set('menu_ct_san_pham', $menu_ct_san_pham);
        
        $has_html = '';
        $this->set('has_html', $has_html);
        $header_menu_sidebar['title'] = $parent_info['category_desc']['title'];
        $header_menu_sidebar['link'] = $parent_info['link']['link'];
        $this->set('header_menu_sidebar', $header_menu_sidebar);
        $menu_sidebar = $this->Category->get_category_by_code($parent_code);
        $this->set('menu_sidebar', $menu_sidebar);
        
        $link_product = $parent_code .'/'. $product_info[0]['product']['product_id'] .'-'. $product_info[0]['link']['link'] .'.html';
        $this->set('link_product', $link_product);
    }
    
    
    function checkout()
    {
        
    }
    
    function favorite()
    {
        $parent_code = $this->parent_code;
        $hsth_code = 'hoa-si-ti-hon';
        $this->set('parent_code', $parent_code);
        $this->set('hsth_code', $hsth_code);
        
        $lang_code = $this->Session->read('lang_code');
        $this->loadModel('Category'); $this->Category->lang_code = $lang_code;
        $this->loadModel('Product'); $this->Product->lang_code = $lang_code;
        
        $favorite = array();
        if($this->Cookie->read('favorite'))
           $favorite = $this->Cookie->read('favorite');
        $this->set('favorite', $favorite);

;        
        $products_favorite = $this->Product->get_product_favorite('cutkit');
        $this->set('products_favorite', $products_favorite);

        
        $parent_id = $this->Category->get_category_id_by_code($parent_code);
        $parent_info = $this->Category->get_info_category_by_id($parent_id);
        $breadcrumbs[] = array('title' => $parent_info['category_desc']['title'], 'link' => $parent_info['link']['link']);
        $breadcrumbs[] = array('title' => 'Sản phẩm yêu thích', 'link' => 'san-pham-yeu-thich');
        $this->set('breadcrumbs', $breadcrumbs);
        
        
    }
}